package offer.offer03;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 36, 100 模仿一下上面的HashMap
 */
public class Solution50 {
    public char firstUniqChar(String s) {
        if(s == null || s.length() == 0) return ' ';
        Map<Character, Integer> chaMap = new LinkedHashMap<>();
        for(int i = 0; i < s.length(); i++){
            char curChar = s.charAt(i);
            if(chaMap.getOrDefault(curChar, -1) == -1){
                chaMap.put(curChar, 1);
            }else {
                chaMap.put(curChar, 2);
            }
        }
        for(Map.Entry<Character, Integer> entry: chaMap.entrySet()){
            //System.out.println(entry);
            if(entry.getValue() == 1){
                return entry.getKey();
            }
        }

        return ' ';
    }

    //这个boolean用得好啊。
    public char firstUniqChar2(String s) {
        HashMap<Character, Boolean> dic = new HashMap<>();
        char[] sc = s.toCharArray();
        for(char c : sc)
            dic.put(c, !dic.containsKey(c));
        for(char c : sc)
            if(dic.get(c)) return c;
        return ' ';
    }

    public char firstUniqChar3(String s) {
        Map<Character, Boolean> dic = new LinkedHashMap<>();
        char[] sc = s.toCharArray();
        for(char c : sc)
            dic.put(c, !dic.containsKey(c));
        for(Map.Entry<Character, Boolean> d : dic.entrySet()){
            if(d.getValue()) return d.getKey();
        }
        return ' ';
    }

    public static void main(String[] args) {
        String input = " ";
        System.out.println(new Solution50().firstUniqChar(input));
    }
}
